import Analysis.General
import Analysis.PreProcess
import Factors
import datetime
import pandas as pd
import Core.Gadget as Gadget

def Screener(database, factors, datetime2):
    #
    factorParams = Factors.FactorParams()

    #
    df = pd.DataFrame()
    for factorName, params in factors.items():
        p = factorParams[factorName]["Period"]
        dfFactor = Analysis.General.Profile(database, datetime2, [factorName])

        # print(dfFactor)
        #
        sort = False
        if "Sort" in params and params["Sort"] == 1:
            sort = True
        dfFactor.sort_values(by=factorName, ascending=sort, inplace=True)
        #
        totalCount = dfFactor.shape[0]
        selectedCount = int(0.1 * totalCount)
        dfSelected = dfFactor[0:selectedCount]
        # print(dfSelected.head(10))
        #
        beforeCount = 0
        if df.empty:
            df = dfSelected
        else:
            beforeCount = len(df)
            df = pd.merge(df, dfSelected, how="inner", on="Symbol")

        # print(df.head(10))
        print("")
        print(factorName, "Before", beforeCount, "After", len(df))
        pass

    #
    print(df)
    pathName = "d://data//FactorAnalysis//"

    df.to_csv(pathName + "Screener_" + Gadget.ToDateString(datetime2) + ".csv")
    pass


def WeightedScreener(database, factors, datetime2, top=0.1):
    #
    def function(x):
        pass
        return x
    #
    dfFactor = Analysis.General.Profile(database, datetime2, list(factors.keys()))
    # print(dfFactor)
    #
    dfFactor = Analysis.PreProcess.Outlier_NSigma(dfFactor, n=3, remove=False)
    # print(dfFactor)
    #
    dfFactor = Analysis.PreProcess.Normalization(dfFactor)
    # dfFactor.to_csv("d:/data/test.csv")
    print(dfFactor)

    #
    #df["Score"] = 0
    #df["Score"] = df.apply(lambda x: function(x), axis=1)


if __name__ == '__main__':
    #
    from Core.Config import *
    cfgPathFilename = os.getcwd() + "/../config.json"
    config = Config(cfgPathFilename)
    database = config.DataBase("MySQL")
    realtime = config.RealTime()

    #
    factors = {}
    #
    # datetime2 = datetime.datetime(2019, 10, 1)
    # factors["Holding_MF_RatioChg_YoY"] = {}
    # factors["Growth_CAGR_ProfitMargin_OperatingProfit_1yr"] = {"Sort": -1}
    # factors["PFCF_LYR"] = {}
    # factors["Growth_CAGR_Sales_1yr"] = {}
    # factors["CashEarningMinusEarning_TTM"] = {"Sort": 1}

    #
    datetime2 = datetime.datetime(2019, 9, 24)
    # factors["PNetCF_R_LYR"] = {}
    # factors["ProfitMargin_NetIncome2"] = {}

    factors["Growth_CAGR_OperatingCashFlow_5Yr"] = {}
    # factors["Growth_YoY_ProfitMargin_OperatingProfit1"] = {}
    # factors["Growth_YoY_OperatingProfit1"] = {}
    # factors["Growth_YoY_ROE"] = {}
    # factors["Growth_YoY_EPS"] = {}
    # factors["Growth_YoY_NetIncome2"] = {}
    # factors["Growth_YoY_OperatingProfit"] = {}
    # factors["Growth_YoY_TotalRevenue"] = {}

    # Screener(database, factors, datetime2)
    WeightedScreener(database, factors, datetime2)


